package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ElectronicExtendOneInfoBo;
import com.hospital.workstation.domain.vo.ElectronicExtendOneInfoVo;
import com.hospital.workstation.domain.ElectronicExtendOneInfo;
import com.hospital.workstation.mapper.ElectronicExtendOneInfoMapper;
import com.hospital.workstation.service.IElectronicExtendOneInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 续一表Service业务层处理
 *
 * @author whb
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class ElectronicExtendOneInfoServiceImpl implements IElectronicExtendOneInfoService {

    private final ElectronicExtendOneInfoMapper baseMapper;

    /**
     * 查询续一表
     */
    @Override
    public ElectronicExtendOneInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询续一表列表
     */
    @Override
    public TableDataInfo<ElectronicExtendOneInfoVo> queryPageList(ElectronicExtendOneInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ElectronicExtendOneInfo> lqw = buildQueryWrapper(bo);
        Page<ElectronicExtendOneInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询续一表列表
     */
    @Override
    public List<ElectronicExtendOneInfoVo> queryList(ElectronicExtendOneInfoBo bo) {
        LambdaQueryWrapper<ElectronicExtendOneInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicExtendOneInfo> buildQueryWrapper(ElectronicExtendOneInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicExtendOneInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonitorId() != null, ElectronicExtendOneInfo::getMonitorId, bo.getMonitorId());
        lqw.eq(bo.getCount() != null, ElectronicExtendOneInfo::getCount, bo.getCount());
        lqw.eq(bo.getBeginTime() != null, ElectronicExtendOneInfo::getBeginTime, bo.getBeginTime());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), ElectronicExtendOneInfo::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceTime()), ElectronicExtendOneInfo::getPlaceTime, bo.getPlaceTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFrequency()), ElectronicExtendOneInfo::getFrequency, bo.getFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getSingleAmount()), ElectronicExtendOneInfo::getSingleAmount, bo.getSingleAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getMenstruumType()), ElectronicExtendOneInfo::getMenstruumType, bo.getMenstruumType());
        lqw.eq(StringUtils.isNotBlank(bo.getSingleMenstruumAmount()), ElectronicExtendOneInfo::getSingleMenstruumAmount, bo.getSingleMenstruumAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getGiveIllnessPathway()), ElectronicExtendOneInfo::getGiveIllnessPathway, bo.getGiveIllnessPathway());
        lqw.eq(StringUtils.isNotBlank(bo.getQuietSpeed()), ElectronicExtendOneInfo::getQuietSpeed, bo.getQuietSpeed());
        lqw.eq(StringUtils.isNotBlank(bo.getQuietContinueTime()), ElectronicExtendOneInfo::getQuietContinueTime, bo.getQuietContinueTime());
        return lqw;
    }

    /**
     * 新增续一表
     */
    @Override
    public Boolean insertByBo(ElectronicExtendOneInfoBo bo) {
        ElectronicExtendOneInfo add = BeanUtil.toBean(bo, ElectronicExtendOneInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改续一表
     */
    @Override
    public Boolean updateByBo(ElectronicExtendOneInfoBo bo) {
        ElectronicExtendOneInfo update = BeanUtil.toBean(bo, ElectronicExtendOneInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicExtendOneInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除续一表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
